/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.behaviours.controller.request;

import jade.core.AID;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

import java.io.IOException;

import ca.usherbrooke.agent.controller.BasicAgentController;
import ca.usherbrooke.agent.message.RequestMessageObjectController;
import ca.usherbrooke.behaviours.BasicRequestBehaviour;
import ca.usherbrooke.behaviours.world.BasicWorldResponder;
import ca.usherbrooke.utils.ExceptionTool;

/**
 * @author Cody Stoutenburg
 * 
 */
public abstract class BasicRequestController extends
		BasicRequestBehaviour<BasicAgentController> {
	private static final long serialVersionUID = 1L;

	private final static MessageTemplate REQUEST_TEMPLATE = MessageTemplate
			.and(MessageTemplate
					.MatchProtocol(BasicWorldResponder.REQUEST_CONTROLLER_PROTOCOL),
					MessageTemplate.MatchPerformative(ACLMessage.INFORM));

	/**
	 * @param agent
	 * @param msg
	 */
	public BasicRequestController(BasicAgentController agent,
			RequestMessageObjectController<?> message) {
		super(agent, generateMessage(agent, agent.getWorldAgent(), message),
				REQUEST_TEMPLATE);
	}

	private static final ACLMessage generateMessage(
			BasicAgentController sender, AID receiver,
			RequestMessageObjectController<?> message) {
		ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
		msg.setSender(sender.getAID());
		msg.addReceiver(receiver);
		msg.setProtocol(BasicWorldResponder.REQUEST_CONTROLLER_PROTOCOL);
		try {
			msg.setContentObject(message);
		} catch (IOException ex) {
			ExceptionTool.showError(ex);
		}
		return msg;
	}
}
